package com.sinitek.web.controller;

import com.sinitek.enums.ApiResultEnum;
import com.sinitek.model.service.BaseEsService;
import com.sinitek.page.Page;
import com.sinitek.web.request.*;
import com.sinitek.web.response.ApiResult;
import com.sinitek.web.response.R;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @program: elastic
 * @description:
 * @author: Mr.chen
 * @create: 2019-04-26 13:44
 */
@Slf4j
public class BaseEsController {

    //索引名称(数据库名)
    private String indexName;
    //类型名称(表名)
    private String type;

    public BaseEsController(String indexName, String type) {
        this.indexName = indexName;
        this.type = type;
    }

    @Autowired
    private BaseEsService baseEsService;

    @ApiOperation(value = "保存文档")
    @PostMapping("/save")
    public ApiResult save(@RequestBody Object t) {
        Object result = baseEsService.save(t, indexName, type);
        if (result == null) {
            return R.error(ApiResultEnum.SUCCESS.getCode(), ApiResultEnum.OPERATE_FAIL.getMsg());
        }
        return R.success(result);
    }

    @ApiOperation(value = "更新文档")
    @PutMapping("/{id}")
    public ApiResult update(@RequestBody Object t,
                            @ApiParam(name = "id", value = "文档编号", required = true) @PathVariable("id") String id) {
        Object result = baseEsService.update(t, indexName, type, id);
        if (result == null) {
            return R.error(ApiResultEnum.SUCCESS.getCode(), ApiResultEnum.OPERATE_FAIL.getMsg());
        }
        return R.success(result);
    }

    @ApiOperation(value = "通过编号获取文档")
    @GetMapping("/{id}")
    public ApiResult get(@ApiParam(name = "id", value = "文档编号", required = true) @PathVariable("id") String id) {
        Object result = baseEsService.findById(indexName, type, id);
        return R.success(result);
    }

    @ApiOperation(value = "通过编号删除文档")
    @DeleteMapping("/{id}")
    public ApiResult findById(@ApiParam(name = "id", value = "文档编号", required = true) @PathVariable("id") String id) {
        Boolean result = baseEsService.deleteById(indexName, type, id);
        if (!result) {
            return R.error(ApiResultEnum.SUCCESS.getCode(), ApiResultEnum.OPERATE_FAIL.getMsg());
        }
        return R.success();
    }

    @ApiOperation(value = "索引查询通过字段全文查询")
    @PostMapping("/findAllMatch")
    public ApiResult findAllMatch(@RequestBody FindAllMatchRequest findAllMatchRequest) {
        Page<Map<String, Object>> result = baseEsService.findAllMatch(indexName,
                findAllMatchRequest.getSortFieldName(),
                findAllMatchRequest.getSortOrder(),
                findAllMatchRequest.getBasePageRequest());
        return R.success(result);
    }

    @ApiOperation(value = "关键词查询(单一字段匹配) (模糊匹配)")
    @PostMapping("/findAllMatchByKeyword")
    public ApiResult findAllMatchByKeyword(@RequestBody FindAllMatchByKeywordRequest findAllMatchByKeywordRequest) {
        Page<Map<String, Object>> result = baseEsService.findAllMatchByKeyword(indexName,
                findAllMatchByKeywordRequest.getSortFieldName(),
                findAllMatchByKeywordRequest.getSortOrder(),
                findAllMatchByKeywordRequest.getSearchFieldName(),
                findAllMatchByKeywordRequest.getBasePageRequest(),
                findAllMatchByKeywordRequest.getKeyWord());
        return R.success(result);
    }

    @ApiOperation(value = "多关键词匹配某一字段查询 (分词精确查询)")
    @PostMapping("/findAllTermsByKeywords")
    public ApiResult findAllTermsByKeywords(@RequestBody FindAllTermsByKeywordsRequest findAllTermsByKeywords) {
        Page<Map<String, Object>> result = baseEsService.findAllTermsByKeywords(indexName,
                findAllTermsByKeywords.getSortFieldName(),
                findAllTermsByKeywords.getSortOrder(),
                findAllTermsByKeywords.getSearchFieldName(),
                findAllTermsByKeywords.getBasePageRequest(),
                findAllTermsByKeywords.getKeyWords().toLowerCase().split(","));
        return R.success(result);
    }

    @ApiOperation(value = "前缀关键词查询 Gar (模糊匹配)")
    @PostMapping("/findAllByPrefix")
    public ApiResult findAllByPrefix(@RequestBody FindAllByPrefixRequest findAllByPrefixRequest) {
        Page<Map<String, Object>> result = baseEsService.findAllByPrefix(indexName,
                findAllByPrefixRequest.getSortFieldName(),
                findAllByPrefixRequest.getSortOrder(),
                findAllByPrefixRequest.getBasePageRequest(),
                findAllByPrefixRequest.getSearchFieldName(),
                findAllByPrefixRequest.getPrefix().toLowerCase());
        return R.success(result);
    }

    @ApiOperation(value = "多字段匹配关键词查询 (模糊匹配-高亮显示)")
    @PostMapping("/findAllMultiMatchByKeywords")
    public ApiResult findAllMultiMatchByKeywords(@RequestBody FindAllMultiMatchByKeywordsRequest findAllMultiMatchByKeywordsRequest) {
        Page<Map<String, Object>> result = baseEsService.findAllMultiMatchByKeywords(indexName,
                findAllMultiMatchByKeywordsRequest.getSortFieldName(),
                findAllMultiMatchByKeywordsRequest.getSortOrder(),
                findAllMultiMatchByKeywordsRequest.getKeyWord().toLowerCase(),
                findAllMultiMatchByKeywordsRequest.getBasePageRequest(),
                findAllMultiMatchByKeywordsRequest.getSearchFieldNames().split(","));
        return R.success(result);
    }


    @ApiOperation(value = "重要 - 范围查询包含(模糊匹配)")
    @PostMapping("/findAllRangeIncludeByCondition")
    public ApiResult findAllRangeIncludeByCondition(@RequestBody FindAllRangeIncludeByConditionRequest findAllRangeIncludeByConditionRequest) {
        Page<Map<String, Object>> result = baseEsService.findAllRangeIncludeByCondition(indexName,
                findAllRangeIncludeByConditionRequest.getSortFieldName(),
                findAllRangeIncludeByConditionRequest.getSortOrder(),
                findAllRangeIncludeByConditionRequest.getBasePageRequest(),
                findAllRangeIncludeByConditionRequest.getSearchFieldName(),
                findAllRangeIncludeByConditionRequest.getFrom(),
                findAllRangeIncludeByConditionRequest.getTo());
        return R.success(result);
    }

    @ApiOperation(value = "重要 - 范围查询不包含 (模糊匹配)")
    @PostMapping("/findAllRangeUnIncludeByCondition")
    public ApiResult findAllRangeUnIncludeByCondition(@RequestBody FindAllRangeUnIncludeByConditionRequest findAllRangeUnIncludeByConditionRequest) {
        Page<Map<String, Object>> result = baseEsService.findAllRangeUnIncludeByCondition(indexName,
                findAllRangeUnIncludeByConditionRequest.getSortFieldName(),
                findAllRangeUnIncludeByConditionRequest.getSortOrder(),
                findAllRangeUnIncludeByConditionRequest.getBasePageRequest(),
                findAllRangeUnIncludeByConditionRequest.getSearchFieldName(),
                findAllRangeUnIncludeByConditionRequest.getFrom(),
                findAllRangeUnIncludeByConditionRequest.getTo());
        return R.success(result);
    }

    @ApiOperation(value = "重要 - 组合查询(精准匹配)")
    @PostMapping("/findAllAssociationByCondition")
    public ApiResult findAllAssociationByCondition(@RequestBody FindAllAssociationByConditionRequest findAllAssociationByConditionRequest) {
        Page<Map<String, Object>> result = baseEsService.findAllAssociationByCondition(indexName,
                findAllAssociationByConditionRequest.getSortFieldName(),
                findAllAssociationByConditionRequest.getSortOrder(),
                findAllAssociationByConditionRequest.getBasePageRequest(),
                findAllAssociationByConditionRequest.getMustQuery(),
                findAllAssociationByConditionRequest.getMustNotQuery(),
                findAllAssociationByConditionRequest.getShouldQuery());
        return R.success(result);
    }

    @ApiOperation(value = "重要 - 关键词模糊查询 (模糊匹配)")
    @PostMapping("/findAllFuzzyByKeyWord")
    public ApiResult findAllFuzzyByKeyWord(@RequestBody FindAllFuzzyByKeyWordRequest findAllFuzzyByKeyWordRequest) {
        Page<Map<String, Object>> result = baseEsService.findAllFuzzyByKeyWord(indexName,
                findAllFuzzyByKeyWordRequest.getSortFieldName(),
                findAllFuzzyByKeyWordRequest.getSortOrder(),
                findAllFuzzyByKeyWordRequest.getBasePageRequest(),
                findAllFuzzyByKeyWordRequest.getSearchFieldName(),
                findAllFuzzyByKeyWordRequest.getKeyWord());
        return R.success(result);
    }

    @ApiOperation(value = "重要 - 关键词通配符查询 * (模糊匹配)")
    @PostMapping("/findAllWildcardByKeyWord")
    public ApiResult findAllWildcardByKeyWord(@RequestBody FindAllWildcardByKeyWordRequest findAllWildcardByKeyWordRequest) {
        Page<Map<String, Object>> result = baseEsService.findAllWildcardByKeyWord(indexName,
                findAllWildcardByKeyWordRequest.getSortFieldName(),
                findAllWildcardByKeyWordRequest.getSortOrder(),
                findAllWildcardByKeyWordRequest.getBasePageRequest(),
                findAllWildcardByKeyWordRequest.getSearchFieldName(),
                findAllWildcardByKeyWordRequest.getKeyWord());
        return R.success(result);
    }

    @ApiOperation(value = "分词精确查询(Object类型)")
    @PostMapping("/findAllCommonTermsByKeywords")
    public ApiResult findAllCommonTermsByKeywords(@RequestBody FindAllCommonTermsByKeywordsRequest findAllCommonTermsByKeywordsRequest) {
        Page<Map<String, Object>> result = baseEsService.findAllCommonTermsByKeywords(indexName,
                findAllCommonTermsByKeywordsRequest.getSortFieldName(),
                findAllCommonTermsByKeywordsRequest.getSortOrder(),
                findAllCommonTermsByKeywordsRequest.getSearchFieldName(),
                findAllCommonTermsByKeywordsRequest.getBasePageRequest(),
                findAllCommonTermsByKeywordsRequest.getKeyWord());
        return R.success(result);
    }

}
